﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "DetailLevelVisibilityFilter.h"
#include "IModelView.h"
#include "DbDataSchema.h"
#include "DbObjectUtils.h"
#include "NdbDataSchema.h"
#include "ModelViewEnums.h"
#include "NdbObjectSchematic.h"
#include "VisibilityTypeEnums.h"
#include "IGraphicsNodeVisibilityFilter.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

DBOBJECT_DATA_DEFINE(DetailLevelVisibilityFilter)
{
}

// 过滤器用视图可见性枚举量作为输入，当前示例只做了按照视图详细等级的可见性过滤，可以根据其他枚举量和视图信息做扩展
DetailLevelVisibilityFilter::DetailLevelVisibilityFilter(VisibilityType detailLevel)
{
    SetDetailLevel__(detailLevel);
}

OwnerPtr<IGraphicsNodeVisibilityFilter> DetailLevelVisibilityFilter::Clone() const
{
    return NEW_AS_OWNER_PTR(DetailLevelVisibilityFilter, GetDetailLevel__());
}

// 只有当视图可见性枚举量包含当前视图的详细等级时，图形节点可见
bool DetailLevelVisibilityFilter::IsVisible(const IModelView* pModelView) const
{
    VisibilityType visType = GetDetailLevel__();
    VisibilityType viewDetailLevel = ConvertModelViewDetailLevelToVisibilityType(pModelView->GetDetailLevel());
    return ((uint32_t)visType & (uint32_t)viewDetailLevel) != 0;
}

// 将视图详细等级枚举量转换为视图可见性枚举量。视图可见性枚举量可做位运算
gcmp::VisibilityType Sample::DetailLevelVisibilityFilter::ConvertModelViewDetailLevelToVisibilityType(gcmp::ModelViewDetailLevel detailLevel) const
{
    switch (detailLevel)
    {
    case gcmp::ModelViewDetailLevel::Coarse:
        return VisibilityType::VisibleInCoarse;

    case gcmp::ModelViewDetailLevel::Medium:
        return VisibilityType::VisibleInMedium;

    case gcmp::ModelViewDetailLevel::Fine:
        return VisibilityType::VisibleInFine;

    case gcmp::ModelViewDetailLevel::Undefined:
    default:
        return VisibilityType::VisibleInAll;
    }
}

bool DetailLevelVisibilityFilter::IsCuttable(const IModelView* pModelView, CutPlaneType cutPlaneType) const
{
    return true;
}

void DetailLevelVisibilityFilter::DeleteReferencedElementIds(const std::vector<ElementId>& ids)
{
    return;
}

bool DetailLevelVisibilityFilter::OnGraphicsNodeTransform(const Matrix4d& matrix)
{
    return true;
}

NdbObject* DetailLevelVisibilityFilter::GetTopOwnerObject() const
{
    return nullptr;
}

